package com.hwadee.ems.controller;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.hwadee.ems.domain.dto.Result;
import com.hwadee.ems.domain.dto.PageQuery;
import com.hwadee.ems.domain.po.PaperApplication;
import com.hwadee.ems.domain.vo.PaperApplicationVO;
import com.hwadee.ems.service.IPaperApplicationService;

/**
 * <p>
 * 系部试卷提交申请 前端控制器
 * </p>
 *
 * @author EnosElinsa
 * @since 2024/06/21
 */
@Slf4j
@RestController
@RequestMapping("/paper_application")
public class PaperApplicationController {

    @Resource
    private IPaperApplicationService paperApplicationService;

    @GetMapping("/page")
    public Result<IPage<PaperApplicationVO>> page(
        PageQuery pageQuery,
        @RequestParam(required = false) String courseName,
        @RequestParam(required = false) String approverName,
        @RequestParam(required = false) Integer approvalStatus,
        @RequestParam(required = false) String className
    ) {
        return paperApplicationService.page(
            pageQuery,
            courseName,
            approverName,
            approvalStatus,
            className
        );
    }

    @PostMapping
    public Result<Long> save(@RequestBody PaperApplication paperApplication) {
        log.info("save paperApplication: " + paperApplication);
        return paperApplicationService.saveWithApproval(paperApplication);
    }

    @PutMapping
    public Result<Void> updateById(@RequestBody PaperApplication paperApplication) {
        if (paperApplicationService.updateById(paperApplication)) {
            log.info("update paperApplication: " + paperApplication);
            return Result.ok();
        } else {
            log.error("update paperApplication failed: " + paperApplication);
            return Result.error();
        }
    }
    
    @DeleteMapping("/{id}")
    public Result<Void> removeById(@PathVariable Long id) {
        if (paperApplicationService.removeById(id)) {
            log.info("remove paperApplication by id: " + id);
            return Result.ok();
        } else {
            log.error("remove paperApplication by id failed: " + id);
            return Result.error();
        }
    }

    @GetMapping("/page_by_applicant_id")
    public Result<IPage<PaperApplicationVO>> pageByApplicantId(
        PageQuery pageQuery,
        @RequestParam Long applicantId,
        @RequestParam(required = false) String courseName,
        @RequestParam(required = false) String approverName,
        @RequestParam(required = false) Integer approvalStatus,
        @RequestParam(required = false) String className
    ) {
        return paperApplicationService.pageByApplicantId(
            pageQuery,
            applicantId,
            courseName,
            approverName,
            approvalStatus,
            className
        );
    }

}